package com.lunm.im.server.handler;

import com.lunm.im.factory.SessionFactory;
import com.lunm.im.massage.LoginRequestMessage;
import com.lunm.im.massage.LoginResponseMessage;
import com.lunm.im.factory.UserServiceFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: mi-server
 * @author: Shawn
 * @create: 2024-12-28 14:33
 */
@Slf4j
@ChannelHandler.Sharable
public class LoginRequestHandler extends SimpleChannelInboundHandler<LoginRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext cxt, LoginRequestMessage msg) throws Exception {
        boolean login = UserServiceFactory.getUserService().login(msg.getUsername(), msg.getPassword());
        LoginResponseMessage message;
        if (login) {
            SessionFactory.getSession().bind(cxt.channel(), msg.getUsername());
            message = new LoginResponseMessage(true, "登录成功");
        } else {
            message = new LoginResponseMessage(false, "用户名或密码不正确");
        }
        cxt.writeAndFlush(message);
    }
}
